package com.poobo.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.poobo.aop.SystemCtrLo4jAop;
import com.poobo.condiction.EqGameInfoCondiction;
import com.poobo.entity.TbEqGamesInfo;
import com.poobo.entity.TbEquipment;
import com.poobo.entity.TbResources;
import com.poobo.service.IEqGamesInfoService;
import com.poobo.service.IEquipmentService;
import com.poobo.service.IResourcesService;
import com.poobo.util.pager.Page;

@Controller
@RequestMapping("eqGamesInfo")
public class EqGamesInfoCtr {
	@Autowired
	private IEqGamesInfoService eqGamesInfoService;
	@Autowired
	private IEquipmentService equipmentService;
	@Autowired
	private IResourcesService resourcesService;
	/**
	 * 设备游戏配置
	 * @param request
	 * @param page
	 * @param condiction
	 * @return
	 */
	@RequestMapping("toEqGamesInfoPage")
	public String toEqGameInfo( HttpServletRequest request, Page page,
			EqGameInfoCondiction condiction ){
		if( condiction.getEquipment() != null && condiction.getEquipment().getNumber() != null ){
			request.setAttribute("number",condiction.getEquipment().getNumber());
		}
		page = eqGamesInfoService.queryEqGamesInfoPage(condiction, page);
		request.setAttribute("pageNo", page.getPageNo());
		request.setAttribute("pageNum", page.getPageNum());
		request.setAttribute("pageSize", page.getPageSize());
		request.setAttribute("eqGameInfo", page.getPageList());
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getPageList();
		Map<String, List<Map<String, Object>>> map =  
				new HashMap<String, List<Map<String, Object>>>();
		Map<String, Object> temp = null;
		String temp_key = null;
		for( int i=0; i < list.size(); i++ ){
			temp = list.get(i);
			temp_key = temp.get("number")+"";
			if(map.containsKey(temp_key)){
				map.get(temp_key).add(temp);
			}
			else{
				List<Map<String, Object>> r_list = new ArrayList<Map<String, Object>>();
				r_list.add(temp);
				map.put(temp_key, r_list);
			}
		}
		request.setAttribute("map", map);
		return "eqGamesInfo/eq_games_info";
	}
	/**
	 * 跳转新增页面
	 * @param request
	 * @return
	 */
	@RequestMapping("toSaveEqGamesInfoPage")
	public String toSaveEqGamesInfoPage(HttpServletRequest request){
		List<TbEquipment> equipments = equipmentService.queryNotConfigGameEqupments();
		List<TbResources> resources = resourcesService.queryAllGames();
		request.setAttribute("equipments", equipments);
		request.setAttribute("resources",resources);
		return "eqGamesInfo/save_eq_games_info";
	}
	/**
	 * 跳转编辑页面
	 * @param request
	 * @param eqGamesInfo
	 * @return
	 */
	@RequestMapping("toEditEqGamesInfoPage")
	public String toEditEqGamesInfoPage( HttpServletRequest request, TbEqGamesInfo eqGamesInfo ){
		Integer id = Integer.parseInt(request.getParameter("eqGamesInfoId"));
		eqGamesInfo = eqGamesInfoService.queryEqGamesInfoById(id);
		request.setAttribute("equip", equipmentService.queryEquipmentById(eqGamesInfo.getEqid()));
		request.setAttribute("resour", resourcesService.queryResourcesById(eqGamesInfo.getReid()));
		request.setAttribute("eqGamesInfo", eqGamesInfo);
		request.setAttribute("equipments", equipmentService.queryNotConfigGameEqupments());
		request.setAttribute("resources", resourcesService.queryAllGames());
		return "eqGamesInfo/edit_eq_games_info";
	}
	/**
	 * 保存
	 * @param eqGamesInfo
	 * @return
	 */
	@RequestMapping("saveEqGamesInfoPage")
	@SystemCtrLo4jAop(description="新增设备游戏配置")
	public String saveEqGamesInfoPage( TbEqGamesInfo eqGamesInfo ){
		if( eqGamesInfo.getIsDefault()==1 ){
			eqGamesInfoService.updateIsDefault(eqGamesInfo.getEqid());
		}
		eqGamesInfoService.saveEqGamesInfo(eqGamesInfo);
		return "redirect:/eqGamesInfo/toEqGamesInfoPage.html";
	}
	/**
	 * 编辑
	 * @param eqGamesInfo
	 * @return
	 */
	@RequestMapping("editEqGamesInfoPage")
	@SystemCtrLo4jAop(description="编辑设备游戏配置")
	public String editEqGamesInfoPage( TbEqGamesInfo eqGamesInfo ){
		if( eqGamesInfo.getIsDefault()==1 ){
			eqGamesInfoService.updateIsDefault(eqGamesInfo.getEqid());
		}
		eqGamesInfoService.saveEqGamesInfo(eqGamesInfo);
		return "redirect:/eqGamesInfo/toEqGamesInfoPage.html";
	}
	/**
	 * 根据ID删除
	 * @param request
	 * @return
	 */
	@RequestMapping("deleteById")
	@SystemCtrLo4jAop(description="删除单个设备游戏配置")
	public String deleteById( HttpServletRequest request ){
		Integer id = Integer.parseInt(request.getParameter("eqGamesInfoId"));
		eqGamesInfoService.deleteEqGamesInfoById(id);
		return "redirect:/eqGamesInfo/toEqGamesInfoPage.html";
	}
	@RequestMapping("deleteByIds")
	@SystemCtrLo4jAop(description="批量删除设备游戏配置")
	public String deleteByIds( HttpServletRequest request ){
		String ids = request.getParameter("ids");
		String[] idss = ids.split(",");
		Integer id = null;
		for (String idStr : idss) {
			id = Integer.parseInt(idStr);
			eqGamesInfoService.deleteEqGamesInfoByEquipmentId(id);
		}
		return "redirect:/eqGamesInfo/toEqGamesInfoPage.html";
	}
	
	@RequestMapping("findEquipmentDefaultGame")
	@ResponseBody
	public String findEquipmentDefaultGame() throws UnsupportedEncodingException{
		List<Map<String, String>> list = eqGamesInfoService.findDefaultGame();
		Map<String, String> map = new HashMap<String, String>(); 
		StringBuffer sbfBuffer = new StringBuffer();
		for( int i=0; i<list.size(); i++ ){
			map = list.get(i);
			String number = map.get("number");
			String resourcesName = map.get("resourcesName");
			String resourcesURL = map.get("resourcesURL");
			if( i==list.size()-1 ){
				sbfBuffer.append(number + "," + resourcesName + "," + resourcesURL );
			}else {				
				sbfBuffer.append(number + "," + resourcesName + "," + resourcesURL + "|");
			}
		}
		return new String(sbfBuffer.toString().getBytes(), "ISO-8859-1");
	}
}
